Module# 05: Linked List Lecture#20: Linked
List with JCF
// Example 20.1:
Create a linked list
// This program
define a linked list and add some items into it.
import java.util.*;
public class CreateLLandAddItems {
public static void main(String args[]) {
// Creating an empty ll of class LinkedList
LinkedList<String>ll = new LinkedList<String>();
// Adding elements to the linked list using a
number of add methods
ll.add("Mumbai");
ll.add("Chennai");
ll.add("Kolkata");
ll.add("Delhi");
ll.add("Bangalore");
ll.add("Guwahati");
ll.add("Hyderabad");
// System.out.println("Linked list :
"+ ll); // Simple printing
// Printing the list using an iterator
Iterator<String>itr=ll.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
// Example 20.2:
Create a linked list with user defined objects
/* This program shows
how to create an array of objects of type Students and then add the array into
a linked list and then printing the same. */
import java.util.*;
//Declaration of a
user defined class
class Student {
String name;
double marks;
// Constructor
Student(String s, double m) {
name = s;
marks = m;
}
void printData () {// To parant a reacord
System.out.print("Name : " + name);
System.out.println(" Marks : " + marks);
}
}
// The main class is
defined below.
public class CreateLLofCollection
{
public static void main(String args[]) {
// Create an aaray of
objects
Student
sArray[] = new Student[5]; // To store 5 objects
// Create the array
sArray
sArray[0] = new Student("Ram", 79.6);
sArray[1] = new Student("Rahim", 85.5);
sArray[2] = new Student("John", 90.1);
sArray[3] = new Student("Lisa", 69.4);
sArray[4] = new Student("Ana", 59.8);
// Creating a
linked-list with sArray collection
LinkedList<Student> ll = new LinkedList<Student>(Arrays.asList(sArray));
Student
temp;
// Printing the list
using an iterator
Iterator<Student>itr=ll.iterator();
while(itr.hasNext()){
temp = itr.next();
temp.printData(); // Print the current
record.
}
}
}
// Example 20.3:
Insertion at different locations
/*This program shows
how items can be inserted at different locations in a linked list. For this
purpose, there are methods like add(), addFirst(), addLast() are defined in the
LinkedList class. */
import java.util.*;
public class LLinsertionDemo {
public static void main(String args[]) {
// Creating an empty
ll of class LinkedList
LinkedList<String>ll = new LinkedList<String>();
// Adding elements to
the linked list using a number of add methods
ll.add("Mumbai"); // Add an initial
item
ll.add("Chennai"); // Add another item
ll.addLast("Kolkata"); // Add at the end
ll.addFirst("Delhi"); // Add at the front
ll.add(2, "Bangalore"); // Add in the specific location
ll.add("Guwahati"); // Sequential add
goes at the end
ll.add("Hyderabad"); // Another sequential
insertion
// Printing the list
using an iterator
Iterator<String>itr=ll.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
// Alternatively:
System.out.println("Linked list :
"+ ll); // Simple printing
}
}
// Example 20.4:
Insertion of a list into a LinkedList
/* A sub list can be
inserted into a linked list in addition to a single item. This program shows
how a sub list can be inserted at different locations in a linked list. For
this purpose, the addAll() method is used.*/
import java.util.*;
public class InsertSubListToLL{
public static void main(String args[]){
LinkedList<String> ll1 = new LinkedList<String>();
System.out.println("Initial list of
elements: "+ ll1);
ll1.add("MP
Allahabad");
ll1.add("MP
Lucknow");
ll1.add("MP
Varanasi");
System.out.println("Initial list: ”
+ ll1);
// Create another linked list, say ll2
LinkedList<String> ll2 = new LinkedList<String>();
ll2.add("MLA Nadia");
ll2.add("MLA
Kharagpur");
//Adding the second
list (ll2) to the first list (ll1)
ll1.addAll(ll2);
System.out.println("After adding
ll2 to ll1: ” + ll1);
// Create another linked list, say ll3
LinkedList<String> ll3 = new LinkedList<String>();
ll3.add("MLA
Durgapur");
ll3.add("MLA
Howrah");
//Inserrt ll3 at a specific position of ll1
ll1.addAll(3, ll3); // Insert ll3 at location 3 of ll1
System.out.println("After insetting
ll3: ” + ll1);
//Do some normal
insertions
ll1.addFirst("President");
ll1.addLast("Prime
Minister");
ll1.add("MP
Chennai");
System.out.println("The final list ”
+ ll1);
}
}
// Example 20.5:
Deletion of an object from a LinkedList
/* Like insertion,
deletion operation on a linked list can be carried our many ways. Following few
examples illustrates the deletion operation with methods remove(),
removeFirst(), removeLast(), etc.*/
import java.util.*;
public class DeletionFromLL {
public static void main(String [] args) {
// Creating a linked
list
LinkedList<String>ll1 = new LinkedList<String>();
ll1.add("A");
ll1.add("E");
ll1.add("I");
ll1.add("O");
ll1.add("U");
ll1.add("H");
System.out.println("List of vowels:
"+ll1);
//Removing specific element from the linked
list
ll1.remove("H"); // Rmovee the vowel H
System.out.println("After deletion
of H : "+ll1);
//Removing element on the basis of specific
position
ll1.remove(0); // This will remove A from the list
System.out.println("After invoking
remove(index) method: "+ll1);
// Let’s create another list of semi-vowels
LinkedList<String> ll2=new LinkedList<String>();
ll2.add("M");
ll2.add("N");
// Adding new
elements to the list of vowels
ll1.addAll(ll2); // Append ll2 after ll1
System.out.println("Updated list :
"+ll1);
//Removing first
element from the list
ll1.removeFirst();
System.out.println("After invoking
removeFirst() method: "+ll1);
//Removing last element from the list
ll1.removeLast();
System.out.println("After invoking
removeLast() method: "+ll1);
// Removing all
elements from ll2
ll1.removeAll(ll2);
System.out.println("After removing
semi-vowels: "+ll1);
ll1.add("A");
ll1.add("B");
ll1.add("A");
//Removing first
occurrence of element from the list
ll1.removeFirstOccurrence("A");
System.out.println("After removing
first occurrence of A: "+ll1);
//Removing the last
occurrence of B
ll1.removeLastOccurrence("B");
System.out.println("After invoking
removeLastOccurrence() method: "+ll1);
//Removing all the
elements present in the list
ll1.clear();
System.out.println("After invoking
clear() method: "+ll1);
}
}
// Example 20.6: Traversal operation on a
linked List collection
/* The following
program illustrates how to traverse two different type of lists in reverse order,
that is, from the end to the front. */
import java.util.*;
public class TravserseReverseLL{
public static void main(String args[]){
// Case 1: a linked
list of countries
LinkedList<String> lCountries = new LinkedList<String>();
lCountries.add("Australia");
lCountries.add("India");
lCountries.add("South
Africa");
lCountries.add("Zimbabwe");
//Traversing the list of countries in reverse
order
Iterator
itr1 =lCountries.descendingIterator();
while(itr1.hasNext()) {
System.out.println(itr1.next());
}
// Case 2: a linked list of numbers
LinkedList<Integer> lNumbers = new LinkedList<Integer>();
lNumbers.add(123);
lNumbers.add(345);
lNumbers.add(567);
lNumbers.add(789);
//Traversing the list
of numbers in reverse order
Iterator
itr2 = lNumbers.descendingIterator();
while(itr2.hasNext()) {
System.out.println(itr2.next());
}
}
}
// Example 20.7:
Miscellaneous operations
/* The
LinkedList class is loaded with several
other methods like get(), contain(), size(), set(), etc. The following program
illustrates those methods and their utilities in Java programming. */
import java.util.*;
public class OtherMethodsOfLL{
public static void main(String args[]){// Creating a linked list
LinkedList<String>lLetters = new LinkedList<String>();
lLetters.add("J");
lLetters.add("O");
lLetters.add("Y");
lLetters.add("W");
lLetters.add("I");
lLetters.add("T");
lLetters.add("H");
lLetters.add("J");
lLetters.add("A");
lLetters.add("V");
lLetters.add("A");
lLetters.add("2020");
System.out.println("List : "+lLetters);
// Finding an
elements in the linked list
boolean status = lLetters.contains("Z");
if(status)
System.out.println("List contains the element 'Z' ");
else
System.out.println("List doesn't contain 'Z'");
// Finding the number of elements in the
linked list
int size = lLetters.size();
System.out.println("Number of
letters = "+ size);
// Get and set
elements from the linked list
String
element = lLetters.get(11);
System.out.println("Element
returned by get() : ” + element);
lLetters.set(11, "The fun");
System.out.println("Linked list
after change : ” + lLetters);
}
}
// Example 20.8: Case
study with a user defined objects
/* The following, a
program shows a list a number of books, insertion of another books in the list,
deletion of books and then traversing of the books, etc.*/
import java.util.*;
// Defining a calls
Book
class Book {
int accnNo;
String title, author, publisher;
double cost;
// Constructor of the class
Book(int id, String t, String auth, String pub, double val) {
accnNo = id;
title = t;
author = auth;
publisher = pub;
cost = val;
}
}
// The main class
maintaining a library of books
public class LibraryLL {
public static void main(String[] args) {
List<Book> library =new LinkedList<Book>(); //Creating list of
Books
//Creating a list of
Books
Book
b1 = new Book(101,"Oracle Java","Leslie Lamport","Oxford",88.5);
Book
b2 = new Book(102,"Complete Java","McGraw Hill", "TMH" , 94);
Book
b3 = new Book(103,"Joy with Java","Samanta","Prentice
Hall",69.6);
library.add(b1); //Adding books to list
library.add(b2);
library.add(b3);
//Traversing the list
for(Book b:library){System.out.println("Book ID: " +b.accnNo);
System.out.print(b.title+" "+b.author+" "+b.publisher+" "+b.cost);
System.out.println();
}
}
}